package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.base.BaseDialog;
import com.mvp.images.WImageLoader;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.MalayAsiaApplication;
import com.wushi.lenovo.asia5b.app_model.good.TaokouModel;
import com.wushi.lenovo.asia5b.app_ui.h5.GoodsDetailsActivity;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

import static java.lang.String.format;

/**
 * Created by zhangyuncai on 2019/4/3.
 * 监听淘口令
 * todo DialogFragment不能在Activity的休眠生命周期调用,否则报 IllegalStateException异常
 */
public class TaokouDialog extends BaseDialog {
    @BindView(R.id.iv_cancelsure_ok)
    ImageView iv_cancelsure_ok;
    @BindView(R.id.iv_comman_img)
    ImageView iv_comman_img;
    @BindView(R.id.tv_comman_name)
    TextView tv_comman_name;
    @BindView(R.id.tv_comman_price)
    TextView tv_comman_price;
    @BindView(R.id.bt_comman_submit)
    Button bt_comman_submit;
    private final TaokouModel model;
    private static TaokouDialog taokouDialog;

    //显示商品详情
    public static void showTaokouDialog(TaokouModel model) {
        Timber.d(format("淘口令5--->>>>>>>>---开始显示淘口令dialog"));
        //栈顶的界面
        Activity endActivity = MalayAsiaApplication.getEndActivity();
        if (endActivity == null) {
            return;
        }
        //保证只有一个在显示
        if (taokouDialog != null && taokouDialog.isShowing()) {
            taokouDialog.dismiss();
        }
        taokouDialog = new TaokouDialog(endActivity, model);
        taokouDialog.show();
    }

    public TaokouDialog(Activity endActivity, TaokouModel model) {
        super(endActivity);
        this.model = model;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_taokou;
    }

    @Override
    protected void initView() {
        setView();
    }

    public void setView() {
        if (model != null && tv_comman_name != null) {
            WImageLoader.displayImage(getContext(), model.getGoods_img(), iv_comman_img);
            tv_comman_name.setText(model.getName());
            if (!TextUtils.isEmpty(model.getPrice_text())) {
                tv_comman_price.setText(model.getPrice_text());
            } else {
                tv_comman_price.setText(format("%s %s", model.getCurrency(), model.getPrice()));
            }

        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.iv_cancelsure_ok, R.id.bt_comman_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancelsure_ok:
                dismiss();
                break;
            case R.id.bt_comman_submit:
                GoodsDetailsActivity.startGoodsDetailsActivity(getContext(), model.getId());
                dismiss();
                break;
        }
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.7f);
    }

}
